/**
 * Service层统一导出
 * 提供新旧架构的兼容性支持
 */

// 现代化Service基础设施
export * from './base';

// Service注册中心
export { ServiceRegistry } from './ServiceRegistry';

// 现代化Service类
export { BagService } from './BagService';
export { RPGService } from './RPGService';
export { ModernTaskService } from './ModernTaskService';
export { EquipService } from './EquipService';
export { default as ModernPropService } from './ModernPropService';

// 兼容性导出 - 单例实例
export { bagService } from './BagService';
export { rpgService } from './RPGService';

// 重新导出其他Service（保持向后兼容）
export { BaseService } from './BaseService';
export { BaseServiceClass } from './BaseServiceClass';

// 其他Service类（暂时保持原样）
export { AdminService } from './AdminService';
export { ClientService } from './ClientService';
export { CreateService } from './CreateService';
export { DieRewardService } from './DieRewardService';
export { FollowService } from './FollowService';
export { IntentService } from './IntentService';
export { MailService } from './MailService';
export { PopupService } from './PopupService';
export { PositionService } from './PositionService';
export { RankingService } from './RankingService';
export { SignService } from './SignService';
export { TalentService } from './TalentService';

// 原始TaskService（保持向后兼容）
export { TaskService } from './TaskService';

/**
 * Service初始化工具函数
 */
import { DIContainer, container } from '../infrastructure/di';
import { ServiceRegistry } from './ServiceRegistry';

/**
 * 初始化所有Service
 * 应用启动时调用此函数
 */
export async function initializeServices(): Promise<void> {
    console.log('Initializing service layer...');

    try {
        // 初始化Service注册中心
        ServiceRegistry.initialize(container);

        // 启动所有Service
        await ServiceRegistry.startAllServices();

        // 进行健康检查
        const healthCheck = await ServiceRegistry.healthCheck();
        if (!healthCheck.healthy) {
            console.warn('Service health check failed:', healthCheck.issues);
        }

        console.log('Service layer initialized successfully');
    } catch (error) {
        console.error('Failed to initialize service layer:', error);
        throw error;
    }
}

/**
 * 销毁所有Service
 * 应用关闭时调用此函数
 */
export async function destroyServices(): Promise<void> {
    console.log('Destroying service layer...');

    try {
        await ServiceRegistry.stopAllServices();
        console.log('Service layer destroyed successfully');
    } catch (error) {
        console.error('Failed to destroy service layer:', error);
        throw error;
    }
}

/**
 * 获取Service状态报告
 */
export function getServiceStatus() {
    return ServiceRegistry.getStatusReport();
}

/**
 * 快速获取Service实例
 */
export const Services = {
    getBagService: () => ServiceRegistry.getService<any>('BagService'),
    getRPGService: () => ServiceRegistry.getService<any>('RPGService'),
    getTaskService: () => ServiceRegistry.getService<any>('TaskService'),
    getEquipService: () => ServiceRegistry.getService<any>('EquipService'),
};

/**
 * Service工厂类
 * 提供Service实例的创建和管理
 */
export class ServiceFactory {
    /**
     * 创建Service实例
     * @param ServiceClass Service类
     * @param container DI容器
     * @returns Service实例
     */
    static create<T>(ServiceClass: new (container?: DIContainer) => T, container?: DIContainer): T {
        return new ServiceClass(container);
    }

    /**
     * 创建并初始化Service实例
     * @param ServiceClass Service类
     * @param container DI容器
     * @returns 已初始化的Service实例
     */
    static async createAndInitialize<T extends { init(): Promise<void> }>(
        ServiceClass: new (container?: DIContainer) => T,
        container?: DIContainer
    ): Promise<T> {
        const instance = new ServiceClass(container);
        await instance.init();
        return instance;
    }
}
